package com.example.shellcollector.ShowApp;

import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceManager;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.shellcollector.Entity.AppDailyUsage;
import com.example.shellcollector.Entity.InstalledAppUsage;
import com.example.shellcollector.database.InstalledAppUsageDao;
import com.example.shellcollector.database.ShellCollectorDatabase;
import com.example.shellcollector.R;
import com.example.shellcollector.util.ShellPreference;
import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter;
import com.google.android.material.appbar.MaterialToolbar;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ShowAppDetails extends AppCompatActivity  {
    private BarChart mBarChart;
    private InstalledAppUsage installedAppUsage;
    private InstalledAppUsageDao installedAppUsageDao;
    private List<AppDailyUsage> appDailyUsages;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_appdetail);

        installedAppUsageDao = ShellCollectorDatabase.getDatabase(getApplicationContext()).getInstalledAppUsageDao();
        MaterialToolbar topAppbar = findViewById(R.id.materialbarindetail);
        topAppbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        Intent intent = getIntent();
        installedAppUsage = intent.getExtras().getParcelable("InstalledAppUsage");
        ImageView imageView = findViewById(R.id.imageViewindetail);
        Drawable d = installedAppUsage.getAppIcon(getPackageManager());
        imageView.setImageDrawable(d);
        TextView textView1 = findViewById(R.id.appnameindetail);
        textView1.setText(installedAppUsage.getAppName());
        TextView textView2 = findViewById(R.id.runningtimeindetail);
        textView2.setText(installedAppUsage.getAppTotalRunningTimeText().substring(0, installedAppUsage.getAppTotalRunningTimeText().length() - 2));
        TextView textView3 = findViewById(R.id.launchtimeindetail);
        textView3.setText("" + installedAppUsage.getLaunchCount());
        BarChart chart = (BarChart) findViewById(R.id.chart);
        BarDataSet barDataSet = new BarDataSet(dataValue(),"DataValue1");
        // barDataSet.setColors(ColorTemplate.COLORFUL_COLORS);

        BarData barData = new BarData();
        barData.addDataSet(barDataSet);
        chart.getDescription().setEnabled(false);
        XAxis xAxis= chart.getXAxis();
        ArrayList<String> xvalues = new ArrayList<>();
        for(int i = 0;i<appDailyUsages.size();i++){
            Date date = new Date(appDailyUsages.get(i).getDate());
            xvalues.add(date.toString().substring(4,10));
        }
        xAxis.setValueFormatter(new IndexAxisValueFormatter(xvalues));
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        xAxis.setGranularity(1f); // only intervals of 1 day
        xAxis.setLabelCount(7);
        YAxis lyAxis = chart.getAxisLeft();
        lyAxis.setAxisMinimum(0f);
        YAxis ryAxis = chart.getAxisRight();
        ryAxis.setAxisMinimum(0f);
        chart.animateX(2000);
        chart.animateY(2000);
        chart.setData(barData);
        chart.invalidate();
    }
    private ArrayList<BarEntry> dataValue(){
        ArrayList<BarEntry> dataValue = new ArrayList<>();
        ShellPreference preference = ShellPreference.getPreference(this);
        long startTime = preference.getStartTime();
        long endTime = preference.getEndTime();
        appDailyUsages = installedAppUsage.getUsageGroupByDay(installedAppUsageDao,startTime,endTime);
        for (int i = 0;i<appDailyUsages.size();i++){
            dataValue.add(new BarEntry(i,appDailyUsages.get(i).getTotalRunningTime()/(1000*60)));
        }
        return dataValue;
    }
}